﻿@using Fluxor.Blazor.Web.Components;
@using SimpleIdServer.IdServer.Helpers;
@using SimpleIdServer.IdServer.Website.Resources;
@using SimpleIdServer.IdServer.Website.Stores.ClientStore;
@using SimpleIdServer.IdServer.Website.Stores.UserStore;
@inherits FluxorComponent
@inject IState<UpdateUserState> updateUserState
@inject IDispatcher dispatcher

<RadzenTemplateForm Submit=@SubmitPassword TItem="PasswordForm" Data=@pwdForm>
    <div>
        <RadzenText TextStyle="TextStyle.Subtitle2" TagName="TagName.H3">@Global.Password</RadzenText>
        <RadzenPassword Name="Password" @bind-Value="@pwdForm.Password" Class="w-100"></RadzenPassword>
        <RadzenRequiredValidator Component="Password" Text="@Global.PasswordRequired"></RadzenRequiredValidator>
    </div>
    <div>
        <RadzenText TextStyle="TextStyle.Subtitle2" TagName="TagName.H3">@Global.PasswordConfirmation</RadzenText>
        <RadzenPassword Name="RepeatPassword" @bind-Value="@pwdForm.RepeatPassword" Class="w-100"></RadzenPassword>
        <RadzenRequiredValidator Component="RepeatPassword" Text="@Global.PasswordRequired"></RadzenRequiredValidator>
        <RadzenCompareValidator Visible=@(!string.IsNullOrEmpty(pwdForm.RepeatPassword)) Value=@pwdForm.Password Component="RepeatPassword" Text="@Global.SamePassword"></RadzenCompareValidator>
    </div>
    <RadzenButton class="mt-1" Variant="Variant.Flat" ButtonType="ButtonType.Submit" ButtonStyle="ButtonStyle.Success" Text="@(updateUserState.Value.IsUpdating ? Global.Saving : Global.Save)" Disabled="@(updateUserState.Value.IsUpdating)" />
</RadzenTemplateForm>

@code {
    [Parameter]
    public EventCallback<UserCredential> CredentialSaved { get; set; }

    record PasswordForm
    {
        public string Password { get; set; } = null!;
        public string RepeatPassword { get; set; } = null!;
    }

    PasswordForm pwdForm = new PasswordForm();

    async void SubmitPassword(PasswordForm pwdForm)
    {
        await CredentialSaved.InvokeAsync(new UserCredential { Value = pwdForm.Password });
    }
}